package dj.common.lang.utils;

import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.*;

/**
 * @ClassName: ThreadPoolUtils
 * @Description: 线程池工具类
 * @Author: ZJG
 * @Date: 2022/2/11 13:22
 **/
public class ThreadPoolUtils {

    private static final int corePoolSize = 10;

    private static final int maxNumPoolSize = 30;

    private static final long keepAliveTime = 10L;

    private static final TimeUnit unit = TimeUnit.SECONDS;

    private static final BlockingQueue<Runnable> workQuene = new LinkedBlockingQueue<>(100);

    private static ThreadPoolExecutor executor;

    private ThreadPoolUtils() {
        CustomizableThreadFactory customizableThreadFactory = new CustomizableThreadFactory("dj-async-executor-");
        executor = new ThreadPoolExecutor(corePoolSize, maxNumPoolSize, keepAliveTime, unit, workQuene, (ThreadFactory) customizableThreadFactory);
    }

    private static class ThreadPoolUtilsInner {
        private static ThreadPoolUtils instance = new ThreadPoolUtils();
    }

    public static ThreadPoolUtils getInstance() {
        return ThreadPoolUtilsInner.instance;
    }

    public ThreadPoolExecutor getExecutor() {
        return executor;
    }
}
